package com.koushikdutta.async;

import java.nio.ByteBuffer;

public class FilteredDataSink extends BufferedDataSink {
    static final /* synthetic */ boolean $assertionsDisabled = (!FilteredDataSink.class.desiredAssertionStatus());

    public FilteredDataSink(DataSink sink) {
        super(sink);
        setMaxBuffer(0);
    }

    public ByteBufferList filter(ByteBufferList bb) {
        return bb;
    }

    public final void write(ByteBuffer bb) {
        if (!isBuffering() || getMaxBuffer() == Integer.MAX_VALUE) {
            ByteBufferList list = new ByteBufferList();
            byte[] bytes = new byte[bb.remaining()];
            bb.get(bytes);
            if ($assertionsDisabled || bb.remaining() == 0) {
                list.add(ByteBuffer.wrap(bytes));
                super.write(filter(list), true);
                return;
            }
            throw new AssertionError();
        }
    }

    public final void write(ByteBufferList bb) {
        if (!isBuffering() || getMaxBuffer() == Integer.MAX_VALUE) {
            ByteBufferList filtered = filter(bb);
            if ($assertionsDisabled || bb == null || filtered == bb || bb.isEmpty()) {
                super.write(filtered, true);
                if (bb != null) {
                    bb.recycle();
                    return;
                }
                return;
            }
            throw new AssertionError();
        }
    }
}
